package com.xue.controller;

import com.google.gson.Gson;
import com.xue.bean.*;
import com.xue.handler.GraphHandler;
import com.xue.service.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * @program: springboot-outbreak
 * @description: 数据处理
 * @create: 2020-05-27 15:27
 **/
@Controller
public class DataController {
    @Autowired
    private DataService dataService;

    /**
     * 整合页面
     * @param model
     * @return
     */
    @RequestMapping("/")
    public String allList(Model model) {
        List<DataBean> dataList = dataService.list();
        model.addAttribute("dataList", dataList);

        List<MapBean> result = new ArrayList<>();
        for (int i = 0; i < dataList.size(); i++) {
            DataBean dataBean = dataList.get(i);
            MapBean mapBean = new MapBean(dataBean.getArea(), dataBean.getNowConfirm());
            result.add(mapBean);

        }
        model.addAttribute("mapData", new Gson().toJson(result));

        String str = GraphHandler.getData();
        List<GraphBean> list = GraphHandler.getGraphData(str);
        //  进一步改造数据格式
        //  因为前端需要的数据是  x轴所有数据的数组和y轴所有数据的数组

        ArrayList<String> dateList = new ArrayList<>();
        ArrayList<Integer> nowConfirmList = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
            GraphBean graphBean = list.get(i);
            dateList.add(graphBean.getDate());
            nowConfirmList.add(graphBean.getNowConfirm());
        }

        model.addAttribute("dateList", new Gson().toJson(dateList));
        model.addAttribute("nowConfirmList", new Gson().toJson(nowConfirmList));


        List<GraphAddBean> addList = GraphHandler.getGraphAddData(str);

        ArrayList<String> addDateList = new ArrayList<>();
        ArrayList<Integer> addConfirmList = new ArrayList<>();
        ArrayList<Integer> addSuspectList = new ArrayList<>();

        for (int i = 0; i < addList.size(); i++) {
            GraphAddBean graphAddBean = addList.get(i);
            addDateList.add(graphAddBean.getDate());
            addConfirmList.add(graphAddBean.getAddConfirm());
            addSuspectList.add(graphAddBean.getAddSuspect());
        }

        model.addAttribute("addDateList", new Gson().toJson(addDateList));
        model.addAttribute("addConfirmList", new Gson().toJson(addConfirmList));
        model.addAttribute("addSuspectList", new Gson().toJson(addSuspectList));


        List<GraphPieBean> pieList = GraphHandler.getGraphPieData();
        model.addAttribute("pieList", new Gson().toJson(pieList));
        List<GraphColumnarBean> columnarList = GraphHandler.getGraphColumnarData();
        Collections.sort(columnarList);

        ArrayList<String> nameList = new ArrayList<>();
        ArrayList<Integer> fromAbroadList = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            GraphColumnarBean bean = columnarList.get(i);
            nameList.add(bean.getArea());
            fromAbroadList.add(bean.getFromAbroad());
        }

        model.addAttribute("nameList", new Gson().toJson(nameList));
        model.addAttribute("fromAbroadList", new Gson().toJson(fromAbroadList));

        return "allList";
    }




    /**
     * 中国疫情表格
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(Model model){
        List<DataBean> list = dataService.list();
        model.addAttribute("list",list);
        return "list";
    }

    /**
     * 全国现有确认折线图
     * @param model
     * @return
     */
    @RequestMapping("/graph")
    public String graph(Model model){
        List<GraphBean> list = GraphHandler.getGraphData();
        ArrayList<String> date = new ArrayList<>();
        ArrayList<Integer> nowConfirm = new ArrayList<>();

        for (int i = 0;i<list.size();i++){
            GraphBean graphBean = list.get(i);
            date.add(graphBean.getDate());
            nowConfirm.add(graphBean.getNowConfirm());
        }
        model.addAttribute("date",date);
        model.addAttribute("nowConfirm",nowConfirm);
        return "graph";
    }


    /**
     * 新增趋势
     * @param model
     * @return
     */
    @RequestMapping("/graphAdd")
    public String graphAdd(Model model) {
        List<GraphAddBean> list = GraphHandler.getGraphAddData();

        ArrayList<String> dateList = new ArrayList<>();
        ArrayList<Integer> addConfirmList = new ArrayList<>();
        ArrayList<Integer> addSuspectList = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
            GraphAddBean graphAddBean = list.get(i);

            dateList.add(graphAddBean.getDate());

            addConfirmList.add(graphAddBean.getAddConfirm());

            addSuspectList.add(graphAddBean.getAddSuspect());
        }
        model.addAttribute("dateList", new Gson().toJson(dateList));
        model.addAttribute("addConfirmList", new Gson().toJson(addConfirmList));
        model.addAttribute("addSuspectList", new Gson().toJson(addSuspectList));
        return "graphAdd";
    }


    /**
     * 累计确诊 、治愈、死亡
     * @param model
     * @return
     */
    @RequestMapping("/graphCumulative")
    public String GraphCumulativeBean(Model model) {
        List<GraphCumulativeBean> list = GraphHandler.getGraphCumulativeData();

        ArrayList<String> dateList = new ArrayList<>();
        ArrayList<Integer> confirm = new ArrayList<>();
        ArrayList<Integer> dead = new ArrayList<>();
        ArrayList<Integer> heal = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
            GraphCumulativeBean graphCumulativeBean = list.get(i);

            dateList.add(graphCumulativeBean.getDate());
            confirm.add(graphCumulativeBean.getConfirm());
            dead.add(graphCumulativeBean.getDead());
            heal.add(graphCumulativeBean.getHeal());
        }
        model.addAttribute("dateList", new Gson().toJson(dateList));
        model.addAttribute("confirm", new Gson().toJson(confirm));
        model.addAttribute("dead", new Gson().toJson(dead));
        model.addAttribute("heal", new Gson().toJson(heal));
        return "graphCumulative";
    }

    /**
     * 柱状图
     */
    @RequestMapping("/graphColumnar")
    public String graphColumnar(Model model){
        List<GraphColumnarBean> list = GraphHandler.getGraphColumnarData();
        Collections.sort(list);

        ArrayList<String> nameList = new ArrayList<>();
        ArrayList<Integer> fromAbroadList = new ArrayList<>();

        for (int i =0;i<10;i++){
            GraphColumnarBean bean = list.get(i);
            nameList.add(bean.getArea());
            fromAbroadList.add(bean.getFromAbroad());
        }
        model.addAttribute("nameList",new Gson().toJson(nameList));
        model.addAttribute("fromAbroadList",new Gson().toJson(fromAbroadList));
        return "graphColumnar";
    }

    /**
     * 饼状图
     */
    @RequestMapping("/graphPie")
    public String graphPie(Model model){
        List<GraphPieBean> graphPieData = GraphHandler.getGraphPieData();
        model.addAttribute("graphPieData",new Gson().toJson(graphPieData));
        return "graphPie";
    }

    /**
     * 地图
     * @param model
     * @return
     */
    @RequestMapping("/map")
    public String map(Model model) {
        List<DataBean> list = dataService.list();

        List<MapBean> result = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            DataBean dataBean = list.get(i);
            MapBean mapBean = new MapBean(dataBean.getArea(), dataBean.getNowConfirm());
            result.add(mapBean);
        }
        model.addAttribute("mapData", new Gson().toJson(result));
        return "map";
    }
}
